<?php

namespace app\common\business;

use app\common\model\mysql\GoodsSku as GoodsSkuModel;
use think\facade\Log;


class GoodsSku
{

    public $model = null;

    public function __construct()
    {
        $this->model = new GoodsSkuModel();
    }

    public function saveAll($data)
    {
        if (!isset($data['skus']) || !isset($data['goods_id'])) {
            return false;
        }

        $insertData = [];
        foreach ($data['skus'] as $value) {
            $insertData[] = [
                'goods_id' => $data['goods_id'],
                'specs_value_ids' => $value['propvalnames']['propvalids'],
                'price' => $value['propvalnames']['skuSellPrice'],
                'cost_price' => $value['propvalnames']['skuMarketPrice'],
                'stock' => $value['propvalnames']['skuStock'],
            ];
        }

        try {
            $result = $this->model->saveAll($insertData);
        } catch (\Exception $e) {
            Log::error(json($e->getMessage()));
            return false;
        }

        return $result->toArray();
    }
}
